package com.tsystems.skilltest;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @author aesipov
 */
public class TransactionDAO
{
    private static final EntityManager entityManager;

    static {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("skilltest");
        entityManager = emf.createEntityManager();
    }

    public void save(Transaction entity)
    {
        entityManager.getTransaction().begin();
        entityManager.persist(entity);
        entityManager.getTransaction().commit();
    }

    public List<Transaction> getAll()
    {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Transaction> criteria = builder.createQuery(Transaction.class);

        Root<Transaction> address = criteria.from(Transaction.class);
        criteria.select(address);
        return entityManager.createQuery(criteria).getResultList();
    }

}
